/*****************************************************************************
 *  CommLayerLib - TCPServer.java
 *  Copyright (C) 2012  Matteo Piscitelli
 *  E-mail: matteo@picciux.it
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  For further info, check http://code.google.com/p/java-comm-layer/
 *
 *  SVN: $Id: TCPServer.java 24 2012-10-02 16:59:26Z picciux@gmail.com $
 *  
 *****************************************************************************/

package it.picciux.commlayer.win.net;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer extends it.picciux.commlayer.net.TCPServer {
	protected ServerSocket srvSock = null;
	
	public TCPServer() {
	}

	@Override
	public void listen(int port) throws IOException {
		if (listening) return;
		srvSock = new ServerSocket(port);
		listening = true;
		Thread t = new Thread(this);
		t.setName("TCPServerWin listen thread");
		t.start();
	}

	@Override
	protected void closeServer() throws IOException {
		if (srvSock != null) srvSock.close();
		srvSock = null;
	}

	@Override
	protected TCPClient acceptClient() throws IOException {
		Socket cliSock = srvSock.accept();
		if (cliSock == null) throw new IOException("Impossible to accept client");
		
		return new TCPClient(cliSock);
	}

	@Override
	protected boolean checkServerStatus() {
		return (srvSock != null);
	}

}
